<template>
<div class="memberCenter">
    <van-tabs v-model="active" swipeable>
        <van-tab v-for="(item,index) in tabList" :key="index" :title="item.value">

            <centerParter :cardInfo="cardInfo" v-if="active == 0"></centerParter>
            <level v-if="active == 1"></level>

        </van-tab>
    </van-tabs>

    <!-- <div>{{token}}</div> -->

</div>
</template>

<script>
import centerParter from "./components/centerParter.vue"
import level from "./components/level.vue"

import {
    isBind,
    getUserInfo
} from "../../sgApi/index.js"

export default {
    name: "memberCenter",
    components: {
        centerParter,
        level
    },
    created() {

    },
    mounted() {
        history.pushState(null, null, document.URL);

        window.addEventListener('popstate', this.disableBrowserBack);

        this.init()
    },
    destroyed() {

        // 清除popstate事件 否则会影响到其他页面

        window.removeEventListener("popstate", this.disableBrowserBack, false);

    },
    data() {
        return {
            token: "",
            active: 0,
            tabList: [{
                    key: "0",
                    value: "会员中心"
                },
                {
                    key: "1",
                    value: "等级礼遇"
                }
            ],
            cardInfo: {}
        }
    },
    watch: {
        active(newV, oldV) {
            if (newV == 0) {

                this.init()
            }

        }
    },
    methods: {
        disableBrowserBack() {

            history.pushState(null, null, document.URL);

        },

        init() { // 通过code获取 openId等用户信息，/api/user/wechat/login 为后台接口

            // let isBind = localStorage.getItem("isBind")
            // if (isBind == 0) {
            //     this.$router.push({
            //         path: "/login"
            //     })
            // }
            new Promise((resolve, reject) => {

                resolve()

            }).then(() => {

                getUserInfo({
                    customerCrmId: localStorage.getItem("customerCrmId")
                }).then(res => {

                    let {

                        customer_level,
                        customerCardNo,
                        customerJFAmt,
                        consultant,
                        developer
                    } = res

                    localStorage.setItem("customerCardNo", customerCardNo)
                    localStorage.setItem("consultant", consultant)
                    localStorage.setItem("developer", developer)
                    localStorage.setItem("customer_mobile", res.customer_mobile)
                    localStorage.setItem("regist_date", res.regist_date)

                    this.cardInfo = {

                        customerCardNo: customerCardNo || "暂无",
                        customerJFAmt: customerJFAmt || "0",
                        customer_level: customer_level || "0"
                    }

                })

            })

        },
        getUrlParam(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
            var r = window.location.search.substr(1).match(reg)
            if (r != null) return unescape(r[2])
            return null

        }

    }
}
</script>

<style lang="scss" scoped>
@import './member.scss';
</style>
